package com.group.controller;

import com.group.service.UserInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pojo.entity.ImgDo;
import pojo.qo.UserInformation;
import utils.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Api(tags = "用户个人详情")
@RestController
@RequestMapping(value = "/userInformation", produces = "text/html;charset=UTF-8")
public class UserInformationController extends BaseController {

    @Autowired
    private UserInformationService informationService;

    @ApiOperation("查询用户信息")
    @RequestMapping(value = "/findUserById.do")
    @ResponseBody
    public String findUserById(@RequestBody Long userId, HttpServletResponse response) {
        System.out.println("用户详情页：查询信息：" + userId);
        UserInformation userById = informationService.findUserById(userId);
        String s = writerSuccessResult(userById, response);
        System.out.println("用户详情页：查询信息：" + s);
        return s;
    }

    @ApiOperation("修改用户信息")
    @RequestMapping(value = "/updataUser.do")
    @ResponseBody
    public String updataUser(UserInformation user, HttpServletResponse response) {
        System.out.println("修改用户信息.获取前端对象:" + user);
        int i = informationService.updataUser(user);
        System.out.println("用户详情：修改：" + i);
        String s = writerSuccessResult(i, response);
        return s;
    }

    @ApiOperation("图片存入数据库")
    @RequestMapping(value = "/uploudImg.do")
    @ResponseBody
    public String uploudImg(UserInformation user, HttpServletResponse response){
        System.out.println("图片存入数据库:"+user);
        int i = informationService.uploudImg(user);
        System.out.println("图片存入数据库:修改："+i);
        String s = writerSuccessResult(i, response);
        return s;
    }

    @ApiOperation("文件上传")
    @PostMapping(value = "/upload")  //, consumes = "multipart/*",headers = "content-type=multipart/form-data"
    public String upload(@ApiParam(value = "文件") MultipartFile file) {

        System.out.println("文件存入本地资源");
        if (file.isEmpty()) {
            throw new RuntimeException("文件上传失败");
        }

        String str="userImg/";

        //获得文件地址
        String baseDir = "D:\\log\\userImg";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        //获得时间
        String dateStr = dateFormat.format(new Date());
        //目录地址
        String dir = baseDir + File.separator + dateStr;
        str=str+dateStr+"/";
        //判断目录是否存在
        File dirFile = new File(dir);
        if (!dirFile.exists()){
            dirFile.mkdirs();
            ImgDo imgDo = new ImgDo();
            String imgdoPath ="userImg/";
            imgdoPath = imgdoPath+dateStr;
            imgDo.setImgPath(imgdoPath);
            imgDo.setImgName("头像");
            String imgdoTags = imgdoPath+" 头像";
            imgDo.setImgTags(imgdoTags);
            System.out.println(imgDo);
            int i = informationService.saveDateToImgPath(imgDo);
            dirFile = new File(dir);
        }

        //在当前文件下存文件并保证文件名不重复
        //获取文件名的后缀，分割 file.getOriginalFilename()-->原文件名
        String oldFileName = file.getOriginalFilename();
        //分割,拿到文件后缀
        String fileSuffix = oldFileName.substring(oldFileName.lastIndexOf("."), oldFileName.length());
        //获取时间戳，进行拼接-->得到文件名
        String fileName = System.currentTimeMillis() + fileSuffix;
        //文件保存的地址 = 目录地址 + 分割符 + 文件名
        String path = dir + "/" + fileName;
        String urlSql = path.substring(path.lastIndexOf("userImg\\"));

        //文件保存地址
        str=str+fileName;
//        System.out.println("获取到的源文件名"+oldFileName);
        //保存文件
        try {
//            Thumbnails.of(oldFileName).size(400,500).toFile(path);
            file.transferTo(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return str;
    }
}
